package co.crossoft.daoImpl;

import co.crossoft.domain.BomBean;
import co.crossoft.domain.BomComponentBean;
import co.crossoft.domain.MaterialBean;
import co.crossoft.mapper.MaterialConfigMapper;
import co.crossoft.util.JsonConvertUtil;
import co.crossoft.util.JsonReturnMsg;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Snow on 8/16/2017.
 */
@Repository("materialDaoImpl")
public class MaterialDaoImpl {

    @Autowired
    MaterialConfigMapper materialConfigMapper;

    public String getMaterialList(String id,String quid,String plant){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        Map map = new HashMap<>();
        String retString = "";
        if(!id.equals("")){
            map.put("id",id);
        }else if(!quid.equals("")){
            map.put("quid",quid);
        }
        map.put("plant",plant);
        List materialList = materialConfigMapper.materialListById(map);
        jsonReturnMsg.setRetList(materialList);
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String saveMaterialData(MaterialBean materialBean){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if(materialBean.getId().equals("") || materialBean.getPlant().equals("") || materialBean.getVersion().equals("")|| materialBean.getStatusFk().equals("")){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("required column can not be empty");
        }else{
            Map paraMap = new HashMap();
            paraMap.put("id",materialBean.getId());
            paraMap.put("plant",materialBean.getPlant());
            paraMap.put("version",materialBean.getVersion());
            List list = materialConfigMapper.isExistMaterial(paraMap);
            if(list.size()>0){
                materialConfigMapper.updateMaterial(materialBean);
            }else{
                materialConfigMapper.addMaterial(materialBean);
            }
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String deleteMaterial(String quid){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if(quid.equals("")){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("Material unique identification can not be empty!");
        }else{
            materialConfigMapper.deleteMaterial(quid);
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }
}
